package com.mfm.dao.impl;

import com.mfm.bean.Student;
import com.mfm.dao.DBUtils;
import com.mfm.dao.StudentDao;
import com.mfm.util.StudentEnum;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StudentDaoImpl extends DBUtils implements StudentDao {
    @Override
    public List<Student> queryAll(String stuname, String stuno, int sex, int pageIndex, int pageSize) {
        List<Student> list = new ArrayList<>();
        List params = new ArrayList<>();
        try {
            StringBuffer sql = new StringBuffer("select * from student where 1=1 and state != 4 ");
            if (stuname != null && stuname.length() > 0) {
                sql.append("and stuname like ?");
                params.add("%" + stuname + "%");
            }
            if (stuno != null && stuno.length() > 0) {
                sql.append("and stuno =?");
                params.add(stuno);
            }
            if (sex != -1) {
                sql.append("and sex=?");
                params.add(sex);
            }

            //分页
            sql.append(" limit ?,?");
            // limit (pageIndex-1)*pageSize,pageSize
            params.add((pageIndex - 1)*pageSize);
            params.add(pageSize);
            resultSet = query(sql.toString(), params);

            while (resultSet.next()) {
                Student student = new Student();
                student.setStuId(resultSet.getInt("stuid"));
                student.setStuName(resultSet.getString("stuname"));
                student.setStuNo(resultSet.getString("stuno"));
                student.setSex(resultSet.getInt("sex"));
                student.setPhone(resultSet.getString("phone"));
                student.setEmail(resultSet.getString("email"));
                student.setRegistered(resultSet.getString("registered"));
                student.setAddress(resultSet.getString("address"));
                student.setProfession(resultSet.getString("profession"));
                student.setIdNumber(resultSet.getString("idnumber"));
                student.setPolitics(resultSet.getString("politics"));
                student.setRegDate(resultSet.getDate("regdate"));
                student.setState(resultSet.getInt("state"));
                student.setIntroduction(resultSet.getString("introduction"));
                student.setgId(resultSet.getInt("gid"));

                list.add(student);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return list;
    }

    @Override
    public int total(String stuname, String stuno, int sex) {
        int total = 0;
        List params = new ArrayList<>();
        try {
            StringBuffer sql = new StringBuffer("select count(*) from student where 1=1 and state != 4 ");
            if (stuname != null && stuname.length() > 0) {
                sql.append("and stuname like ?");
                params.add("%" + stuname + "%");
            }
            if (stuno != null && stuno.length() > 0) {
                sql.append("and stuno =?");
                params.add(stuno);
            }
            if (sex != -1) {
                sql.append("and sex=?");
                params.add(sex);
            }
            resultSet = query(sql.toString(), params);
            while (resultSet.next()) {
                total=resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll();
        }
        return total;
    }

    @Override
    public int update(Student student) {
        int update=0;
        try {
            String sql ="insert into student values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            List params=new ArrayList();
            params.add(student.getStuName());
            params.add(student.getStuNo());
            params.add(student.getSex());
            params.add(student.getPhone());
            params.add(student.getEmail());
            params.add(student.getRegistered());
            params.add(student.getAddress());
            params.add(student.getProfession());
            params.add(student.getIdNumber());
            params.add(student.getPolitics());
            params.add(new Date());
            params.add(StudentEnum.READING.type);
            params.add(student.getIntroduction());
            params.add(student.getgId());
            update=update(sql,params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public Student findById(int sid) {
        Student student = new Student();
        try {
            String sql = "select * from student where stuid=?";
            List params = new ArrayList<>();
            params.add(sid);
            resultSet = query(sql, params);
            while (resultSet.next()){
                student.setStuId(resultSet.getInt("stuid"));
                student.setStuNo(resultSet.getString("stuno"));
                student.setStuName(resultSet.getString("stuname"));
                student.setSex(resultSet.getInt("sex"));
                student.setPhone(resultSet.getString("phone"));
                student.setProfession(resultSet.getString("profession"));
                student.setAddress(resultSet.getString("address"));
                student.setRegDate(resultSet.getDate("regdate"));
                student.setEmail(resultSet.getString("email"));
                student.setIntroduction(resultSet.getString("introduction"));
                student.setgId(resultSet.getInt("gid"));
                student.setRegistered(resultSet.getString("registered"));
                student.setIdNumber(resultSet.getString("idnumber"));
                student.setPolitics(resultSet.getString("politics"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return student;
    }

    @Override
    public int updateStu(Student student) {
        int update=0;
        try {
            String sql="update student set stuName=?,stuNo=?,sex=?,phone=?,email=?,registered=?,address=?,profession=?,idNumber=?,politics=?,regDate=?,state=?,introduction=?,gId=? where stuId=?";
            List params=new ArrayList();
            params.add(student.getStuName());
            params.add(student.getStuNo());
            params.add(student.getSex());
            params.add(student.getPhone());
            params.add(student.getEmail());
            params.add(student.getRegistered());
            params.add(student.getAddress());
            params.add(student.getProfession());
            params.add(student.getIdNumber());
            params.add(student.getPolitics());
            params.add(new Date());
            params.add(StudentEnum.READING.type);
            params.add(student.getIntroduction());
            params.add(student.getgId());
            params.add(student.getStuId());
            update=update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public int deleteStu(String sid) {
        int update = 0;
        try {
            String sql="update student set state=? where stuId=? ";
            List params=new ArrayList();
            params.add(StudentEnum.DELETE.type);
            params.add(sid);
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public boolean selectByStuNo(String stuNo) {
        boolean orNot=true;
        try {
            String sql="select stuno from student where stuno = ? ";
            List params=new ArrayList<>();
            params.add(stuNo);
            resultSet = query(sql, params);
            if (resultSet.next()){
                orNot=false;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return orNot;
    }
}
